// @ 516
// _(:з」∠)_

define([ ], function() {
    'use strict';

    let weatherStation = {
        init: function( param ) {
            let _this = this,
                parent = param.parent || document.querySelector(".main"),
                thisElmt = parent.querySelector(".weatherStation"),
                thisView = `<div class="weatherStation">
                                    <div class="ws_monitor">
                                        <div class="ws_head">
                                            <h5 class="ws_title">小气候站监测</h5>
                                            <div name="select" class="station">
                                                <h></h> <ul></ul>
                                            </div>
                                            <span class="ws_close" type="plane"></span>
                                        </div>
                                        <div class="ws_main"></div>
                                    </div>
                                    <div class="ws_elmtChart">
                                        <div class="ws_head">
                                            <h5 class="ws_title">相关要素监测</h5>
                                            <span class="ws_close" type="layer"></span>
                                        </div>
                                        <div class="ws_main">
                                            <ul class=""></ul>
                                            <div class=""></div>
                                        </div>
                                    </div>
                                </div>`;
            
            if ( thisElmt ) thisElmt.remove();
            parent.insertAdjacentHTML( "beforeEnd", thisView );
            thisElmt = parent.querySelector(".weatherStation");

            // 关闭面板
            let closePanel = thisElmt.querySelector(".ws_monitor .ws_close");
            closePanel.addEventListener( 'click', function(e) {
                thisElmt.remove();
                
                return false;
            }, false);

            _this.initMonitorTree({ parent: thisElmt.querySelector(".ws_monitor .ws_main") });
        },
        initMonitorTree: function( param ) {
            let _this = this,
            thisView = `<ul class="monitorTree">
                                <li>
                                    <p type="eda">
                                        <span type="42">风向: <h></h> ( ° )</span>
                                        <span type="41">风速: <h></h> ( m/s )</span>
                                    </p>
                                    <p type="pre">
                                        <span type="44">雨量: <h></h> ( mm )</span>
                                    </p>
                                </li>
                                <li>
                                    <p type="">
                                        <span type="">日照时数: <h></h> ( h )</span>
                                        <span type="45">辐照度: <h></h> ( W/m² )</span>
                                    </p>
                                    <p type="erh">
                                        <span type="36">湿度: <h></h> ( % )</span>
                                    </p>
                                </li>
                                <li>
                                    <p type="tmp">
                                        <span type="">气温: <h></h> ( ℃ )</span>
                                    </p>
                                </li>
                                <li>
                                    <p type="">
                                        <span type="34">土壤温度: <h></h> ( ℃ )</span>
                                    </p>
                                    <p type="">
                                        <span type="">土壤湿度: <h></h> ( % )</span>
                                    </p>
                                </li>
                                <li>
                                    <p type="">
                                        <span type="34">土壤温度: <h></h> ( ℃ )</span>
                                    </p>
                                    <p type="">
                                        <span type="">土壤湿度: <h></h> ( % )</span>
                                    </p>
                                </li>
                            </ul>`;

            param.parent.textContent = "";
            param.parent.insertAdjacentHTML( "beforeEnd", thisView );

            RST.weatherStation(
                _this.getRequstUrl({
                    type: "detail",
                    // stationId: 7406, // 地下
                    stationId: 7408 // 地面
                }),
            function( result ) {
                let timeLess = 0 * 24 * 60 * 60 * 1000;

                // 请求地面监测数据
                result.nodes["1"].params.forEach( item => {
                    _this.getElmtData({
                        sn: result.sn,
                        nodes: "1",
                        params: item,
                        begin: formatTime( '', 'day_', (-timeLess) ),
                        end: formatTime( '', 'day_' ),
                        parent: param.parent
                    });
                });
            });
        },
        initElmtChart: function() {
            let _this = this;
        },
        getElmtData: function( param ) {
            let _this = this;
            RST.weatherStation(
                _this.getRequstUrl({
                    type: "deviceData",
                    sn: param.sn,
                    nodes: param.nodes,
                    params: param.params,
                    begin: param.begin,
                    end: param.end
                }),
            function( result ) {
                let view = param.parent.querySelector("span[type='"+ param.params +"'] h"),
                    text = result[Object.keys(result)[Object.keys(result).length - 1]][param.nodes][param.params];
                if ( !view ) text = "无数据";
                view.textContent = text;
            });
        },
        getRequstUrl: function( param ) {
            let _this = this,
                thisUrl = null;

            switch( param.type ) {
                case "detail":
                    thisUrl = "/v2/poi/detail?id="+ param.stationId;
                break;
                case "paramInfo":
                    thisUrl = "/v2/param?id="+ param.paramId;
                break;
                case "allParamInfo":
                    thisUrl = "/v2/param/all";
                break;
                case "deviceData":
                    thisUrl = "/v2/poi/device/data?sn="+ param.sn +"&nodes="+ param.nodes +"&params="+ param.params
                                    +"&begin="+ param.begin +"&end="+ param.end;
                break;
            }

            return thisUrl;
        }
    };

    window.weatherStation = weatherStation;
    
    return weatherStation;
});